package leetcode.editor.cn.q1_300.q;

/**
 * @author qidi
 * @date 2021-11-03 10:30:52
 */
class Q0026_RemoveDuplicatesFromSortedArray {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int removeDuplicates(int[] nums) {
            if (nums.length == 0 || nums.length == 1) return nums.length;

            int i = 0;
            int j = 1;
            int sameLen = 0;
            int len = nums.length;
            while (i <= j && j < len) {
                if (nums[i] != nums[j]) {
                    if (j > i + 1) {
                        System.arraycopy(nums, j, nums, i + 1, len - j);
                        len -= j - i - 1;
                        j -= sameLen;
                        sameLen = 0;
                    }
                    i++;
                } else {
                    sameLen++;
                }
                j++;
            }
            return len - sameLen;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0026_RemoveDuplicatesFromSortedArray().new Solution();
        System.out.println(solution.removeDuplicates(new int[]{1, 1}));
        System.out.println(solution.removeDuplicates(new int[]{1, 1, 2}));
        System.out.println(solution.removeDuplicates(new int[]{1, 1, 2, 2}));
        System.out.println(solution.removeDuplicates(new int[]{1, 2, 3}));
        System.out.println(solution.removeDuplicates(new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}));
    }
}